// 性格分析功能
function initCharacterModule() {
  const characterForm = document.getElementById('characterForm');
  if (!characterForm) return;
  
  characterForm.addEventListener('submit', function(e) {
    e.preventDefault();
    
    // 检查登录状态
    if (checkNeedLogin()) return;
    
    const baziId = document.getElementById('characterBaziId').value;
    if (!baziId) {
      showToast('请先生成八字');
      return;
    }
    
    // 获取性格分析
    characterAPI.getCharacterAnalysis(userToken, baziId)
      .then(response => {
        if (response.code === 200) {
          // 显示基础分析结果
          showCharacterResult(response.data);
        } else {
          showToast(response.message || '获取性格分析失败');
        }
      })
      .catch(error => {
        showToast(error.message || '获取性格分析失败');
      });
  });
  
  // 查看详细分析按钮
  const viewDetailBtn = document.getElementById('viewCharacterDetailBtn');
  if (viewDetailBtn) {
    viewDetailBtn.addEventListener('click', function() {
      const baziId = document.getElementById('characterBaziId').value;
      if (!baziId) {
        showToast('请先生成八字');
        return;
      }
      
      // 检查是否需要支付
      if (checkNeedPayment(false, 1.1)) {
        // 设置支付相关信息
        document.getElementById('paymentType').value = 'character';
        document.getElementById('productId').value = baziId;
        return;
      }
      
      // 获取详细分析
      characterAPI.getCharacterDetail(userToken, baziId)
        .then(response => {
          if (response.code === 200) {
            showCharacterDetailResult(response.data);
          } else {
            showToast(response.message || '获取详细分析失败');
          }
        })
        .catch(error => {
          showToast(error.message || '获取详细分析失败');
        });
    });
  }
  
  // 获取今日运势按钮
  const getDailyFortuneBtn = document.getElementById('getDailyFortuneBtn');
  if (getDailyFortuneBtn) {
    getDailyFortuneBtn.addEventListener('click', function() {
      // 检查登录状态
      if (checkNeedLogin()) return;
      
      // 获取今日运势
      characterAPI.getDailyFortune(userToken)
        .then(response => {
          if (response.code === 200) {
            showDailyFortune(response.data);
          } else {
            showToast(response.message || '获取今日运势失败');
          }
        })
        .catch(error => {
          showToast(error.message || '获取今日运势失败');
        });
    });
  }
}

// 显示性格分析结果
function showCharacterResult(data) {
  const resultContainer = document.getElementById('characterResultContainer');
  if (!resultContainer) return;
  
  resultContainer.style.display = 'block';
  
  const characterInfo = document.getElementById('characterInfo');
  const characterAnalysis = document.getElementById('characterAnalysis');
  
  if (characterInfo) {
    characterInfo.innerHTML = `
      <p class="text-lg font-medium">您的性格类型：${data.characterType}</p>
      <p class="mt-2">五行属性：${data.fiveElements}</p>
      <p>阴阳属性：${data.yinYang}</p>
    `;
  }
  
  if (characterAnalysis) {
    characterAnalysis.innerHTML = `
      <p class="mt-4">${data.basicAnalysis}</p>
      <p class="mt-2 text-gray-500">查看详细分析可获得更多性格特质解读</p>
    `;
  }
}

// 显示性格详细分析结果
function showCharacterDetailResult(data) {
  const detailContainer = document.getElementById('characterDetailContainer');
  if (!detailContainer) return;
  
  detailContainer.style.display = 'block';
  
  const detailContent = document.getElementById('characterDetailContent');
  if (detailContent) {
    detailContent.innerHTML = `
      <h3 class="text-xl font-bold mb-4">详细性格分析</h3>
      <div class="space-y-4">
        <div>
          <h4 class="text-lg font-medium text-indigo-700">性格特质</h4>
          <p>${data.traits}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">思维方式</h4>
          <p>${data.thinkingStyle}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">情感表达</h4>
          <p>${data.emotionalExpression}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">人际交往</h4>
          <p>${data.socialInteraction}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">工作风格</h4>
          <p>${data.workStyle}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">适合职业</h4>
          <p>${data.suitableCareers}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">潜在挑战</h4>
          <p>${data.challenges}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">成长建议</h4>
          <p>${data.growthSuggestions}</p>
        </div>
      </div>
    `;
  }
}

// 显示今日运势
function showDailyFortune(data) {
  const fortuneContainer = document.getElementById('dailyFortuneContainer');
  if (!fortuneContainer) return;
  
  fortuneContainer.style.display = 'block';
  
  const fortuneContent = document.getElementById('dailyFortuneContent');
  if (fortuneContent) {
    fortuneContent.innerHTML = `
      <h3 class="text-xl font-bold mb-4">今日运势 (${data.date})</h3>
      <div class="space-y-4">
        <div>
          <h4 class="text-lg font-medium text-indigo-700">综合运势</h4>
          <div class="flex items-center">
            <div class="text-yellow-500">${'★'.repeat(data.overallRating)}</div>
            <div class="text-gray-300">${'★'.repeat(5 - data.overallRating)}</div>
          </div>
          <p class="mt-2">${data.overallDesc}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">事业运势</h4>
          <div class="flex items-center">
            <div class="text-yellow-500">${'★'.repeat(data.careerRating)}</div>
            <div class="text-gray-300">${'★'.repeat(5 - data.careerRating)}</div>
          </div>
          <p class="mt-2">${data.careerDesc}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">财运</h4>
          <div class="flex items-center">
            <div class="text-yellow-500">${'★'.repeat(data.wealthRating)}</div>
            <div class="text-gray-300">${'★'.repeat(5 - data.wealthRating)}</div>
          </div>
          <p class="mt-2">${data.wealthDesc}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">感情运势</h4>
          <div class="flex items-center">
            <div class="text-yellow-500">${'★'.repeat(data.loveRating)}</div>
            <div class="text-gray-300">${'★'.repeat(5 - data.loveRating)}</div>
          </div>
          <p class="mt-2">${data.loveDesc}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">健康提示</h4>
          <p>${data.healthTips}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">幸运色</h4>
          <p>${data.luckyColor}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">吉祥方位</h4>
          <p>${data.luckyDirection}</p>
        </div>
      </div>
    `;
  }
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
  initCharacterModule();
});
